//
//  recommendTableViewCell.m
//  BabyAndBaby
//
//  Created by 尹宁 on 16/8/1.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "recommendTableViewCell.h"
#import "PhotoCollectionViewCell.h"

@implementation recommendTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    _addButton.hidden = YES;
    // Initialization code
    if (_collectionView != nil) {
        _collectionView = nil;
    }
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _margin = 4;
    _itemWH = (357 - 2 * _margin - 4) / 3 - _margin;
    layout.itemSize = CGSizeMake(100, 100 );
    layout.minimumInteritemSpacing = _margin;
    layout.minimumLineSpacing = _margin;
    NSInteger i = _imaArr.count % 3 ? _imaArr.count / 3 + 1: _imaArr.count / 3;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.contentInset = UIEdgeInsetsMake(4 * self.bounds.size.width / 375, 0, 0, 2 * self.bounds.size.height / 667);
    _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2 * self.bounds.size.height / 667);
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.contentView addSubview:_collectionView];
    [_collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"PhotoCollectionViewCell"];
    _collectionView.hidden = YES;

}

//- (void)setImaArr:(NSMutableArray *)imaArr

- (void)setImaArr:(NSMutableArray *)imaArr
{
    _imaArr = [NSMutableArray arrayWithArray:imaArr];
    if (imaArr.count != 0) {
//        if (_collectionView == nil) {
            // 照片加载
        NSInteger i = _imaArr.count % 3 ? _imaArr.count / 3 + 1: _imaArr.count / 3;
//        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20 * self.bounds.size.width / 375, _MerchantCodeTextfield.frame.origin.y + _MerchantCodeTextfield.frame.size.height + 20, 320 * self.bounds.size.width / 375, 108 * i) collectionViewLayout:layout];
        _collectionView.frame = CGRectMake(20 * self.bounds.size.width / 375, _MerchantCodeTextfield.frame.origin.y + _MerchantCodeTextfield.frame.size.height + 20, 320 * self.bounds.size.width / 375, 108 * i);
        _collectionView.hidden = NO;
        [_collectionView reloadData];

//        }
    } else {
//        _collectionView.frame = CGRectMake(0, 0, 0, 0);

        _collectionView.hidden = YES;
    }


}



#pragma mark UICollectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    if (self.imaArr.count >= 6) {
//        return self.imaArr.count;
//    }else{
        return self.imaArr.count;
//    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollectionViewCell" forIndexPath:indexPath];
//        if (indexPath.row == self.imaArr.count) {
////            cell.delegateBtn.hidden = YES;
////            cell.imageView.image = [UIImage imageNamed:@"添加图片"];
//        } else {
        cell.delegateBtn.hidden = NO;
        cell.delegateBtn.tag = indexPath.item + 1000;
        [cell.delegateBtn addTarget:self action:@selector(delegateImage:) forControlEvents:UIControlEventTouchUpInside];
        cell.imageView.image = self.imaArr[indexPath.row];
//        }
        return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
        
        if (indexPath.row == self.imaArr.count)
        {
            //        [self takePhotoOrSelectAlbum];
        }else{
            //这里点击图片看大图
        }
    
}


//删除图片
- (void)delegateImage:(UIButton *)sender
{
    
//    [self.imaArr removeObjectAtIndex:sender.tag];
//    [_collectionView reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"delete" object:nil userInfo:@{@"row":[NSNumber numberWithInteger:sender.tag - 1000], @"section":_str}];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
